let basicUrl = "";

if (process.env.NODE_ENV === "development") {
  // 开发环境
  basicUrl = "https://tcb-ybhq45ructtaual2247c0-3b5456.service.tcloudbase.com";
} else {
  // 生产环境
  basicUrl = "https://tcb-ybhq45ructtaual2247c0-3b5456.service.tcloudbase.com";
}

// 解决后端传null 问题
const replaceNull = (obj) => {
  for (let key in obj) {
    switch (Object.prototype.toString.call(obj[key]).slice(8, -1)) {
      case "Object":
        replaceNull(obj[key]);
        break;
      case "Array":
        for (let i = 0; i < obj[key].length; i++) {
          replaceNull(obj[key][i]);
        }
        break;
      default:
        if (obj[key] === null || obj[key] == "null") obj[key] = undefined;
    }
  }
};

export function request(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: basicUrl + data.url, //真实接口地址。
      method: data.method || "get", //请求的方式
      data: data.data || {}, //参数
      header: {
        Authorization: "",
        "content-type": data.type ? data.type : "application/json",
      },
      // 成功使用resolve
      success: (res) => {
        resolve(res.data);
      },
      //失败调用reject
      fail: (err) => {
        reject(err);
      },
    });
  });
}
